<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%--
  Created by IntelliJ IDEA.
  User: 86157
  Date: 2022/2/7
  Time: 21:46
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>生产计划单</title>
</head>
<body>
<div class="container" id="ylmapply">
    <div class="row">
        <div style="position: absolute; right: 10%">
            <input type="button" onclick="opendfile()" class="btn btn-primary" value="添加产品" id="tianjiamapply">
            <input type="button" class="btn btn-primary" value="删除产品" onclick="deledefile()" id="delmapply">
            <input type="button" class="btn btn-primary" value="预览" id="yulan" onclick="djyl()">
            <input type="button" class="btn btn-primary" value="确认" id="qrmapplylist" style="display: none">
            <input type="button" class="btn btn-primary" value="返回" id="fhmapply" onclick="returnmapply()" style="display: none">
        </div><br><br><br>
            <div class="container">
                <div style="position: absolute; left: 40%"><h4>生产计划单</h4></div><br><br><br>
                    <div>
                        生产理由：<input type="text" readonly="readonly" style="border: none" value="新发生">
                        供货时间:<input type="date" id="ghsj" class="input_id" >
                    </div> <br>

                    登记人:<input type="text" width="500px" style="border-left-style: none;border-right-style: none;border-top-style: none" value="newer">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    登记时间:<input type="text" id="dengji" style="border: none">
                <table id="applylist" style="width: 900px; height: 50px;">

                </table>
                <div style="position: relative;top: -500px">
                    备注：<input type="text" style="width: 900px;height: 80px;"></div>
                </div>
        </div>
    </div>


    <!-- 模态框  选择物料  指向 childdemo页面-->
    <div id="opendfile" class="modal">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-body">
                </div>
            </div>
        </div>
    </div>
</div>


<script>
    var params_mapply={
        data:[],
        uniqueId:"productId",
        columns:[
            {
                title: "点选",
                field: "ck",
                checkbox:true
            },
            {
                title: "产品名称",
                field: "productName"
            },
            {
                title: "产品编号",
                field: "productId"
            },
            {
                title: "描述",
                field: "productDescribe"
            },
            {
                title: "数量",
                field: "amount",
                formatter:function (value,row,index) {
                    /* return "<input data-id='input" + index + "'  type='text' class='form-control' value='"+value+"'/>"*/
                    return "<input name='amount' onkeyup='this.value=this.value.replace(/\D/g,1)' min='1' type='number' style='width: 50px;' value='1'/>"
                }
            },
            {
                title: "单位",
                field: "amountUnit"
            },
            {
                title: "单价(元)",
                field: "costPrice",
                formatter:function (value,row,index) {
                    return "<label class='costPrice'>"+value+"</label>"
                }
            },
            {
                title: "小计(元)",
                field: "",
                formatter: function (value, row, index) {
                    return "<span id='span" + index + "'>"+value+"</span>";
                }
            }
        ]
    };
    $("#applylist").bootstrapTable(params_mapply);

    //登记时间
    var dDate = new Date();
    var sDate = dDate.getFullYear() + "/" + (dDate.getMonth() + 1) + "/" + dDate.getDate();
    sDate = sDate + " " + dDate.getHours() + ":" + dDate.getMinutes() + ":" + dDate.getSeconds();
    $("#dengji").val(sDate);

    var rows;
    //添加产品
    function opendfile() {
        $.showDialogBoxByUrl("产品",
            "${pageContext.request.contextPath}/mapply/addmapply.jsp",{},
            function (data) {
                if (data==1){
                    var rows2=$("#admapply").bootstrapTable("getAllSelections");
                    rows=[];
                    $.each(rows2,function (index,item) {
                        var temp={"Id":item.Id,"productName":item.productName,"costPrice":item.costPrice};
                        rows.push(temp)
                    })
                    $("#applylist").bootstrapTable("load",rows);
                }
            },"md")
    }

    //删除产品
    function deledefile() {
        var rows4 = $("#applylist").bootstrapTable("getAllSelections");
        for (var i =0;i<rows4.length;i++) {
            $("#applylist").bootstrapTable("removeByUniqueId", rows4[i].productId);
        }
    }

    var rows=[];
    function djyl() {
        if (rows.length>0){
            $("#yulan").hide();
            $("#tianjiamapply").hide();
            $("#delmapply").hide();
            $("#qrmapplylist").show();
            $("#fhmapply").show();
        }else {
            alert("请先选择产品")
        }
    }
    /*计算小计*/
    $("#applylist").on("input",function (e) {

        var num=$(e.target).val()

        var price=$(e.target).parent().next().next().text();

        $(e.target).parent().next().next().next().text(num*parseFloat(price));
        countNum();

    })

    //确认
    $("#qrmapplylist").click(function () {
        var applys=[];
        //组装生产计划表里面的明细
        var input = $("#applylist input[type='number']");
        var rows=$("#applylist").bootstrapTable("getData");

        //去获取表格里面的数据
        $.each(rows, function (index, item) {

            var apply=$("#admapply").serializeObject();
            apply["productName"]=item.productName;
            apply["productId"]=item.productId;
            apply["remark"]=item.remark;
            apply["amount"]=input.eq(index).val();
            apply["listPrice"]=item.listPrice;
            applys.push(apply)
        });
    })

    //返回
    function returnmapply() {
        $("#yulan").show();
        $("#tianjiamapply").show();
        $("#delmapply").show();
        $("#qrmapplylist").hide();
        $("#fhmapply").hide();
    }
</script>
</body>
</html>
